<?php

class SF_AttrBindCategory_Adminhtml_BindController extends Mage_Adminhtml_Controller_Action
{
    protected function _isAllowed()
    {
        return true;
    }
    
	protected function _initAction()
	{
		$this->loadLayout()
			 ->_setActiveMenu('vendors/attrbindcategory')
			 ->_title($this->__('Manage Attribute Sets'))
			 ->_addBreadcrumb($this->__('Items Manager'), $this->__('Item Manager'));
		
		return $this;
	}
 
	public function indexAction()
	{
		$this->_initAction()
			 ->renderLayout();
	}

	public function newAction()
	{
        $this->_forward('edit');
    }

    public function editAction()
    {
	 	$id    = $this->getRequest()->getParam('id');
     	$model = Mage::getModel('attrbindcategory/set')->load($id);
        
	 	if ($model->getId() || $id == 0)
	 	{
    		$data = Mage::getSingleton('adminhtml/session')->getFormData(true);

	        if (!empty($data))
	        {
	            $model->setData($data);
			}
	        Mage::register('attrbindcategory_data', $model);

	        $this->loadLayout();
			$this->_setActiveMenu('attrbindcategory/items');
			$this->_title($this->__('Manage Attribute Sets'));
	        $this->_addBreadcrumb(
				$this->__('Item Manager'),         
				$this->__('Item Manager')
	        );
	        $this->_addBreadcrumb(
	            $this->__('Item Attribute Sets'),    
	            $this->__('Item Attribute Sets')
	        );

	        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
	        $this->_addContent($this->getLayout()
	        	 ->createBlock('attrbindcategory/adminhtml_list_edit'))
	        	 ->_addLeft($this->getLayout()
	        	 ->createBlock('attrbindcategory/adminhtml_list_edit_tabs'));
            $this->renderLayout();

        }
        else
        {
            Mage::getSingleton('adminhtml/session')->addError(
              	$this->__('Item does not exist')
            );
            $this->_redirect('*/*/');
        }
    }




    public function saveAction(){
    	if ($data = $this->getRequest()->getPost()) {
		 	$model = Mage::getModel('attrbindcategory/set');        
            $model->setData($data)->setId($this->getRequest()->getParam('id'));
            try {
                if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                    $model->setCreatedTime(now())
                        ->setUpdateTime(now());
                } else {
                    $model->setUpdateTime(now());
                }
                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setFormData(false); 
             	if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
    	}
    	Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to find item to save'));
        $this->_redirect('*/*/');
    }
    public function deleteAction() {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $model = Mage::getModel('attrbindcategory/set');
                 
                $model->setId($this->getRequest()->getParam('id'))
                    ->delete();
                     
                Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Item was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }


    public function massDeleteAction() {
        $webIds = $this->getRequest()->getParam('attrbindcategory');
        if(!is_array($webIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($webIds as $webId) {
                    $attrset = Mage::getModel('attrbindcategory/set')->load($webId);
                    $attrset->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    $this->__(
                        'Total of %d record(s) were successfully deleted', count($webIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }
    
    public function massStatusAction()
    {
        $webIds = $this->getRequest()->getParam('attrbindcategory');
        if(!is_array($webIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($webIds as $webId) {
                    $attrset = Mage::getSingleton('attrbindcategory/set')
                        ->load($webId)
                        ->setStatus($this->getRequest()->getParam('status'))
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d record(s) were successfully updated', count($webIds))
                );
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }



}